package protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

public class MessageDecoder extends ReplayingDecoder<Void> {

    /* 解码方法
     * channelHandlerContext 上下文对象
     * byteBuf  入站的byteBuf对象
     * list 将解码后的数据传递给下一个hander
     * */
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("调用解码方法 MessageDecoder decode");
        int length = byteBuf.readInt();//获取长度
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        //封装成MessageProtocol对象，传递给下一个hander
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(length);
        messageProtocol.setContent(bytes);
        list.add(messageProtocol);
    }
}
